package com.gitee.feizns.quickstart.ding.talk.api.v10.yida.forms.instances;

import com.aliyun.dingtalkyida_1_0.Client;
import com.aliyun.dingtalkyida_1_0.models.GetFormDataByIDHeaders;
import com.aliyun.dingtalkyida_1_0.models.GetFormDataByIDRequest;
import com.aliyun.dingtalkyida_1_0.models.GetFormDataByIDResponse;
import com.aliyun.dingtalkyida_1_0.models.GetFormDataByIDResponseBody;
import com.aliyun.teautil.models.RuntimeOptions;
import com.gitee.feizns.quickstart.ding.talk.client.Clients;
import lombok.SneakyThrows;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

/**
 * 查询表单数据（/v1.0/yida/forms/instances/{id}）
 * 接口路径：<a href="https://open.dingtalk.com/document/orgapp/query-form-data">查询表单数据（/v1.0/yida/forms/instances/{id}）</a>
 * @link <a href="https://open.dingtalk.com/document/orgapp/query-form-data">服务端API 宜搭应用开发 表单 查询表单数据</a>
 * @author feizns
 * @since 2024/3/18
 */
public class GetById {

    /**
     * 执行根据编号获取表单实例数据
     * @param accessToken 访问凭据
     * @param id 表单实例编号
     * @param req {@link GetFormDataByIDRequest}
     * @return {@link GetFormDataByIDResponseBody}
     */
    @SneakyThrows
    public static GetFormDataByIDResponseBody execute(@NotNull String accessToken,
                                                      @NotNull String id,
                                                      @Nullable GetFormDataByIDRequest req) {
        Client client = Clients.get(Client.class);
        GetFormDataByIDResponse data = client.getFormDataByIDWithOptions(id, req, headers(accessToken), new RuntimeOptions());
        return data.getBody();
    }

    /**
     * 执行根据编号获取表单实例数据
     * @param accessToken 访问凭据
     * @param id 表单实例编号
     * @param req {@link GetFormDataByIDRequest}
     * @return {@link GetFormDataByIDResponseBody}
     */
    @SneakyThrows
    public static GetFormDataByIDResponseBody execute(@NotNull String accessToken,
                                                      @NotNull String id,
                                                      @NotNull String appType,
                                                      @NotNull String systemToken,
                                                      @NotNull String userId,
                                                      @NotNull String language) {
        GetFormDataByIDRequest req = new GetFormDataByIDRequest();
        req.setAppType(appType);
        req.setSystemToken(systemToken);
        req.setUserId(userId);
        req.setLanguage(language);
        return execute(accessToken, id, req);
    }

    /**
     * @param accessToken 访问凭据
     * @return {@link GetFormDataByIDHeaders}
     */
    public static GetFormDataByIDHeaders headers(@NotNull String accessToken) {
        GetFormDataByIDHeaders getFormDataByIDHeaders = new GetFormDataByIDHeaders();
        getFormDataByIDHeaders.xAcsDingtalkAccessToken = accessToken;
        return getFormDataByIDHeaders;
    }

}
